home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / misc / dial.lha / Dial / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-02  |  20.1 KB  |  579 lines

  1.  
  2. /* /// "header"  */
  3. /* Main-Header File inserted by GenCodeC */
  4. /* Libraries */
  5. #include <libraries/mui.h>
  6. #include <libraries/gadtools.h> /* for BARLABEL in MenuItem */
  7.  
  8. /* Prototypes */
  9. #ifdef __GNUC__
  10. #include <proto/muimaster.h>
  11. #include <proto/exec.h>
  12. #include <proto/alib.h>
  13. #include <proto/dos.h>
  14. #else
  15. #include <clib/muimaster_protos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/alib_protos.h>
  18. #include <clib/dos_protos.h>
  19. #include <pragmas/muimaster_pragmas.h>
  20. #endif /* __GNUC__ */
  21.  
  22. /*  Ansi  */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25.  
  26. /* Increase stack size */
  27. LONG __stack=8192;
  28. /* GenCodeC header end */
  29.  
  30. /* Include generated by GenCodeC */
  31. #include "GUI.h"
  32.  
  33. /* --- own --- */
  34. #include <workbench/workbench.h>
  35. #include <workbench/startup.h>
  36. #include "dialtone.h"
  37. #include "listhooks.h"
  38. #include "_cat.h"
  39.  
  40. /* ---------- */
  41.  
  42. int PBchanged = 0;
  43. int StartIconified = 0;
  44.  
  45. /* Declarations for libraries (inserted by GenCodeC) */
  46. struct Library * IntuitionBase=0;
  47. struct Library * MUIMasterBase=0;
  48. struct Library * LocaleBase=0;
  49. struct Library * IconBase=0;
  50.  
  51. /* /// */
  52.  
  53. /* /// "Arexx Hooks"  */
  54. struct Hook RexxHook;
  55.  
  56. APTR func_RexxHook(struct Hook *a0, APTR a2, APTR **a1)
  57. {
  58.         /* a0->h_Data is a pointer to App
  59.            a1 is an array of the Arexx parameters
  60.            a1[0] is a pointer to the number string given to the arexx DIAL function */
  61.     Dial(a0->h_Data, a1[0], 0);
  62.  
  63.     return NULL;
  64. }
  65. /* /// */
  66.  
  67.  
  68. /* /// "CopyList"  */
  69. void CopyList(struct ObjApp *App)
  70. /* copies entries from phonebook list to dial list */
  71. {
  72.     int i;
  73.     LONG l, m;
  74.     BOOL diff = FALSE;
  75.     struct ListEntry *LE, *LE2;
  76.  
  77.         /* check if both lists are identical */
  78.     get(App->LV_main, MUIA_List_Entries, &l);
  79.     get(App->LV_set, MUIA_List_Entries, &m);
  80.     if (l!=m) diff=TRUE;
  81.  
  82.     if (!diff && l>0)
  83.         for (i=0;;i++)
  84.         {
  85.             DoMethod(App->LV_main,MUIM_List_GetEntry,i,&LE);
  86.             DoMethod(App->LV_set,MUIM_List_GetEntry,i,&LE2);
  87.             if (!LE) break;
  88.  
  89.             if (strcmp(LE->Name,LE2->Name)!=0) diff=TRUE;
  90.             if (strcmp(LE->Number,LE2->Number)!=0) diff=TRUE;
  91.         };
  92.  
  93.  
  94.     if (diff)                           /* copy list, if lists are different */
  95.     {
  96.         set(App->LV_main,MUIA_List_Quiet,TRUE);
  97.  
  98.             /* remove all entries from dial-list */
  99.         DoMethod(App->LV_main,MUIM_List_Clear);
  100.  
  101.             /* copy entries from phonebook-list */
  102.         for (i=0;;i++)
  103.         {
  104.             DoMethod(App->LV_set,MUIM_List_GetEntry,i,&LE);
  105.             if (!LE) break;
  106.             DoMethod(App->LV_main,MUIM_List_InsertSingle,LE,MUIV_List_Insert_Bottom);
  107.         }
  108.  
  109.         set(App->LV_main,MUIA_List_Quiet,FALSE);
  110.     }
  111.  
  112.         /* activate the entry, that is active in phonebook-list */
  113.     get(App->LV_set,MUIA_List_Active, &l);
  114.     if (l<0) l=0;
  115.     set(App->LV_main,MUIA_List_Active, l);
  116. }
  117. /* /// */
  118.  
  119. /* /// "LoadPhoneBook"  */
  120. int LoadPhoneBook(struct ObjApp *App)
  121. {
  122.     int i;
  123.     struct ListEntry LE;
  124.     BPTR f;
  125.     char *name;
  126.     char *number;
  127.     char *str;
  128.  
  129.     get(App->STR_PA_pbname, MUIA_String_Contents, &str);
  130.  
  131.     f=Open(str,MODE_OLDFILE);
  132.     if (!f) {free(str); return FALSE; }
  133.  
  134.     str=malloc(128);
  135.     str=FGets(f,str,128);
  136.     if (strncmp(str,"Dial Phonebook",14)!=0)
  137.     {
  138.         free(str);
  139.         return FALSE;
  140.     };
  141.     free(str);
  142.  
  143.     set(App->LV_set,MUIA_List_Quiet,TRUE);
  144.     DoMethod(App->LV_set,MUIM_List_Clear);
  145.  
  146.     name=malloc(128);
  147.     number=malloc(128);
  148.  
  149.     while (1)
  150.     {
  151.         name=FGets(f,name,63);
  152.         number=FGets(f,number,63);
  153.  
  154.         if (!number) break;
  155.  
  156.         if (name[strlen(name)-1]=='\n') name[strlen(name)-1]=0;
  157.         if (number[strlen(number)-1]=='\n') number[strlen(number)-1]=0;
  158.  
  159.         strcpy(LE.Name,name);
  160.         strcpy(LE.Number,number);
  161.  
  162.         DoMethod(App->LV_set,MUIM_List_InsertSingle, &LE, MUIV_List_Insert_Bottom);
  163.     }
  164.  
  165.     Close(f);
  166.  
  167.     free(name);
  168.     free(number);
  169.  
  170.     set(App->LV_set,MUIA_List_Quiet,FALSE);
  171.     set(App->LV_set,MUIA_List_Active,MUIV_List_Active_Top);
  172.  
  173.     CopyList(App);
  174.     PBchanged=0;
  175.  
  176.     return TRUE;
  177. }
  178. /* /// */
  179.  
  180. /* /// "SavePhoneBook"  */
  181. int SavePhoneBook(struct ObjApp *App)
  182. {
  183.     int i;
  184.     BPTR f;
  185.     char *str = malloc(128);
  186.  
  187.     get(App->STR_PA_pbname, MUIA_String_Contents, &str);
  188.  
  189.     f=Open(str,MODE_NEWFILE);
  190.     free(str);
  191.     if (!f) return FALSE;
  192.  
  193.     FPuts(f,"Dial Phonebook - do not edit this file\n");
  194.  
  195.     for (i=0; ;i++)
  196.     {
  197.         struct ListEntry *LE;
  198.  
  199.         DoMethod(App->LV_set,MUIM_List_GetEntry,i,&LE);
  200.         if (!LE) break;
  201.  
  202.         FPuts(f,LE->Name); FPutC(f,'\n');
  203.         FPuts(f,LE->Number); FPutC(f,'\n');
  204.     }
  205.  
  206.     Close(f);
  207.     PBchanged=0;
  208.     return TRUE;
  209. }
  210. /* /// */
  211.  
  212.  
  213. /* /// "init"  */
  214. /* Init() function */
  215. void init( void )
  216. {
  217.         if (!(IntuitionBase = OpenLibrary("intuition.library",37)))
  218.         {
  219.                 printf("Can't Open Intuition Library\n");
  220.                 exit(20);
  221.         }
  222.         if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  223.         {
  224.                 printf("Can't Open MUIMaster Library\n");
  225.                 CloseLibrary(IntuitionBase);
  226.                 exit(20);
  227.         }
  228.         if (!(IconBase = OpenLibrary("icon.library",0)))
  229.         {
  230.                 printf("Can't Open Icon Library\n");
  231.         }
  232.         if (!(LocaleBase = OpenLibrary("locale.library",38)))
  233.         {
  234.                 printf("Can't Open Locale Library\n");
  235.                 printf("Built-in Language will be used !!!\n");
  236.         }
  237.         OpenAppCatalog(NULL,NULL);
  238. }
  239. /* GenCodeC init() end */
  240. /* /// */
  241.  
  242. /* /// "end"  */
  243. /* End() function */
  244. void end( int err )
  245. {
  246.         CloseAppCatalog();
  247.         if (LocaleBase)
  248.                 CloseLibrary(LocaleBase);
  249.         if (IconBase)
  250.                 CloseLibrary(IconBase);
  251.         CloseLibrary(MUIMasterBase);
  252.         CloseLibrary(IntuitionBase);
  253.  
  254.         exit(err);
  255. }
  256. /* GenCodeC end() end */
  257. /* /// */
  258.  
  259. /* /// "ReadToolTypes"  */
  260. ReadToolTypes(struct WBStartup *wbmsg)
  261. {
  262.     struct WBArg *wbarg;
  263.     int i;
  264.  
  265.     for (i=0, wbarg=wbmsg->sm_ArgList; i<wbmsg->sm_NumArgs; i++, wbarg++)
  266.     {
  267.         LONG olddir = -1;
  268.         struct DiskObject *diskobj;
  269.  
  270.             /* if there's a directory lock for this wbarg, CD there */
  271.         if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  272.             olddir = CurrentDir(wbarg->wa_Lock);
  273.  
  274.         if((*wbarg->wa_Name) && (diskobj=GetDiskObject(wbarg->wa_Name)))
  275.         {
  276.             char **toolarray;
  277.             char *s;
  278.  
  279.             toolarray = (char **)diskobj->do_ToolTypes;
  280.  
  281.             if(s=(char *)FindToolType(toolarray,"CX_POPUP"))
  282.                 if(MatchToolValue(s,"NO")) StartIconified=1;
  283.  
  284.                 /* Free the diskobject we got */
  285.             FreeDiskObject(diskobj);
  286.         }
  287.         if(olddir != -1)  CurrentDir(olddir); /* CD back where we were */
  288.     }
  289. }
  290. /* /// */
  291.  
  292. /* /// "main" */
  293. /* Main Function inserted by GenCodeC */
  294. int main(int argc,char **argv)
  295. {
  296.         struct ObjApp * App = NULL;     /* Object */
  297.         BOOL    running = TRUE;
  298.         ULONG   signal;
  299.         LONG b;
  300.         struct MUI_Command commands[2] = { {"dial","NUMBER",1,&RexxHook},
  301.                                            {0,0,0,0} };
  302.  
  303.         /* Program initialisation : generated by GenCodeC */
  304.         init();
  305.  
  306.             /* from Workbench */
  307.         if (IconBase && argc==0) ReadToolTypes((struct WBStartup*)argv);
  308.  
  309.         /* Create Object : generated by GenCodeC */
  310.         if (!(App = CreateApp(StartIconified)))
  311.         {
  312.                 printf("Can't Create App\n");
  313.                 end(20);
  314.         }
  315.  
  316.         DoMethod(App->App,MUIM_Application_Load,MUIV_Application_Load_ENVARC);
  317.  
  318.         LoadPhoneBook(App);
  319.         set(App->LV_set,MUIA_List_Active,MUIV_List_Active_Top);
  320.         set(App->LV_main,MUIA_List_Active,MUIV_List_Active_Top);
  321.         set(App->window,MUIA_Window_ActiveObject, App->LV_main);
  322.  
  323.         get(App->CH_system, MUIA_Selected, &b);
  324.         if (!b) set(App->STR_system, MUIA_Disabled, TRUE);
  325.  
  326.         get(App->LV_set, MUIA_List_Entries, &b);
  327.         if (b==0)
  328.         {
  329.             set(App->BT_Dial, MUIA_Disabled, TRUE);
  330.             set(App->STR_Name, MUIA_Disabled, TRUE);
  331.             set(App->STR_Number, MUIA_Disabled, TRUE);
  332.             set(App->BT_Remove, MUIA_Disabled, TRUE);
  333.             set(App->BT_Sort, MUIA_Disabled, TRUE);
  334.             set(App->BT_Save, MUIA_Disabled, TRUE);
  335.             set(App->LV_main, MUIA_Disabled, TRUE);
  336.             set(App->LV_set, MUIA_Disabled, TRUE);
  337.         }
  338.  
  339.             /* Install Arexx-Hook */
  340.         InstallHook(&RexxHook, func_RexxHook, App);
  341.         set(App->App,MUIA_Application_Commands,commands);
  342.  
  343.         while (running)
  344.         {
  345.                 switch (DoMethod(App->App,MUIM_Application_Input,&signal))
  346.                 {
  347.                         case MEN_QUIT :
  348.                         case MUIV_Application_ReturnID_Quit:
  349.                                 {
  350.                                     get(App->App,MUIA_Application_ForceQuit,&b);
  351.  
  352.                                     if (PBchanged && !b)
  353.                                     {
  354.                                         if (MUI_Request(App->App,App->window,0,"Dial",
  355.                                                         GetDialString(MSG_Quit_BT),
  356.                                                         GetDialString(MSG_Quit))==1)
  357.                                             running = FALSE;
  358.                                     }
  359.                                     else
  360.                                         running=FALSE;
  361.                                 }
  362.                                     break;
  363.  
  364.                         case MEN_ABOUT :
  365.                                 {
  366.                                         DoMethod(App->App,OM_ADDMEMBER, App->aboutwin);
  367.                                         DoMethod(App->aboutwin,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  368.                                                  App->App,2,MUIM_Application_ReturnID,ID_ABOUT_CLOSE);
  369.                                         DoMethod(App->A_BT_OK,MUIM_Notify,MUIA_Pressed,FALSE,
  370.                                                  App->App,2,MUIM_Application_ReturnID,ID_ABOUT_CLOSE);
  371.                                         set(App->aboutwin,MUIA_Window_Open, TRUE);
  372.                                 }
  373.                                     break;
  374.  
  375.                         case ID_ABOUT_CLOSE :
  376.                                 {
  377.                                         set(App->aboutwin,MUIA_Window_Open, FALSE);
  378.                                         DoMethod(App->App, OM_REMMEMBER, App->aboutwin);
  379.                                 }
  380.                                     break;
  381.  
  382.                         case ID_ABOUT_IMAGECLICK : Dial(App,"0190331331",0); break;
  383.  
  384.                         case MEN_ABOUTMUI :
  385.                                     {
  386.                                         if (!App->muiaboutwin)
  387.                                             App->muiaboutwin = AboutmuiObject,
  388.                                               MUIA_Window_RefWindow, App->window,
  389.                                               MUIA_Aboutmui_Application, App->App,
  390.                                             End;
  391.  
  392.                                         if (App->muiaboutwin) set(App->muiaboutwin,MUIA_Window_Open,TRUE);
  393.                                     } break;
  394.  
  395.                         case ID_Dial :
  396.                                 {
  397.                                     LONG i;
  398.                                     struct ListEntry *LE;
  399.  
  400.                                     get(App->LV_main, MUIA_List_Active, &i);
  401.                                     DoMethod(App->LV_main,MUIM_List_GetEntry,i,&LE);
  402.  
  403.                                     set(App->App,MUIA_Application_Sleep,TRUE);
  404.                                     Dial(App,LE->Number,0);
  405.                                     set(App->App,MUIA_Application_Sleep,FALSE);
  406.                                 }
  407.                                     break;
  408.  
  409.                         case ID_ListClick :
  410.                                 {
  411.                                     LONG i, page;
  412.                                     struct ListEntry *LE;
  413.                                     struct Object *Obj;
  414.  
  415.                                         /* fill in Name and Number string gadgets,
  416.                                            when user selects entry in listview */
  417.                                     get(App->LV_set, MUIA_List_Active, &i);
  418.                                     DoMethod(App->LV_set,MUIM_List_GetEntry,i,&LE);
  419.                                     set(App->STR_Name, MUIA_String_Contents, LE->Name);
  420.                                     set(App->STR_Number, MUIA_String_Contents, LE->Number);
  421.  
  422.                                         /* if there are no entries in list, disable some objects.
  423.                                            enable them, if there are entries */
  424.                                     get(App->LV_set, MUIA_List_Entries, &i);
  425.                                     if (i==0)
  426.                                     {
  427.                                         set(App->BT_Dial, MUIA_Disabled, TRUE);
  428.                                         set(App->STR_Name, MUIA_Disabled, TRUE);
  429.                                         set(App->STR_Number, MUIA_Disabled, TRUE);
  430.                                         set(App->BT_Remove, MUIA_Disabled, TRUE);
  431.                                         set(App->BT_Sort, MUIA_Disabled, TRUE);
  432.                                         set(App->BT_Save, MUIA_Disabled, TRUE);
  433.                                         set(App->LV_main, MUIA_Disabled, TRUE);
  434.                                         set(App->LV_set, MUIA_Disabled, TRUE);
  435.                                     }
  436.                                     else
  437.                                     {
  438.                                         set(App->BT_Dial, MUIA_Disabled, FALSE);
  439.                                         set(App->STR_Name, MUIA_Disabled, FALSE);
  440.                                         set(App->STR_Number, MUIA_Disabled, FALSE);
  441.                                         set(App->BT_Remove, MUIA_Disabled, FALSE);
  442.                                         set(App->BT_Sort, MUIA_Disabled, FALSE);
  443.                                         set(App->BT_Save, MUIA_Disabled, FALSE);
  444.                                         set(App->LV_main, MUIA_Disabled, FALSE);
  445.                                         set(App->LV_set, MUIA_Disabled, FALSE);
  446.                                     }
  447.  
  448.                                     get(App->GR_grp_0,MUIA_Group_ActivePage,&page);
  449.                                     get(App->window, MUIA_Window_ActiveObject, &Obj);
  450.  
  451.                                     if (Obj!=App->STR_Name && Obj!=App->STR_Number && page==1)
  452.                                         set(App->window, MUIA_Window_ActiveObject, App->STR_Name);
  453.  
  454.                                 }
  455.                                     break;
  456.  
  457.                         case ID_DialPage :
  458.                                     {
  459.                                         CopyList(App);
  460.                                             /* make dial-list active object for keyboard input */
  461.                                         set(App->window,MUIA_Window_ActiveObject, App->LV_main);
  462.                                     }
  463.                                         break;
  464.  
  465.                         case ID_Name :
  466.                                 {
  467.                                     LONG i;
  468.                                     struct ListEntry *LE;
  469.                                     char *name;
  470.  
  471.                                         /* get selected ListEntry */
  472.                                     get(App->LV_set, MUIA_List_Active, &i);
  473.                                     DoMethod(App->LV_set,MUIM_List_GetEntry,i,&LE);
  474.  
  475.                                     if (!LE) break;
  476.  
  477.                                     get(App->STR_Name, MUIA_String_Contents, &name);
  478.  
  479.                                     if (strcmp(LE->Name,name)!=0)
  480.                                     {
  481.                                             /* change name */
  482.                                         strcpy(LE->Name,name);
  483.                                             /* redraw it */
  484.                                         DoMethod(App->LV_set,MUIM_List_Redraw,MUIV_List_Redraw_Active);
  485.  
  486.                                         PBchanged=1;
  487.                                     }
  488.                                 }
  489.                                     break;
  490.  
  491.                         case ID_Number :
  492.                                 {
  493.                                     LONG i;
  494.                                     struct ListEntry *LE;
  495.                                     char *number;
  496.  
  497.                                         /* get selected ListEntry */
  498.                                     get(App->LV_set, MUIA_List_Active, &i);
  499.                                     DoMethod(App->LV_set,MUIM_List_GetEntry,i,&LE);
  500.  
  501.                                     if (!LE) break;
  502.  
  503.                                     get(App->STR_Number, MUIA_String_Contents, &number);
  504.  
  505.                                     if (strcmp(LE->Number,number)!=0)
  506.                                     {
  507.                                             /* change number */
  508.                                         strcpy(LE->Number,number);
  509.                                             /* redraw it */
  510.                                         DoMethod(App->LV_set,MUIM_List_Redraw,MUIV_List_Redraw_Active);
  511.  
  512.                                         PBchanged=1;
  513.                                     }
  514.                                 }
  515.                                     break;
  516.  
  517.                         case ID_Add:
  518.                                 {
  519.                                     struct ListEntry LE;
  520.  
  521.                                     memset(&LE,0,sizeof(struct ListEntry));
  522.                                     DoMethod(App->LV_set,MUIM_List_InsertSingle,&LE,MUIV_List_Insert_Bottom);
  523.                                     set(App->LV_set,MUIA_List_Active,MUIV_List_Active_Bottom);
  524.                                     set(App->window, MUIA_Window_ActiveObject, App->STR_Name);
  525.                                 }
  526.                                   break;
  527.  
  528.  
  529.                         case MEN_LOAD : case ID_LoadPB :
  530.                                   {
  531.                                       int b;
  532.  
  533.                                       if (PBchanged)
  534.                                       {
  535.                                           if (MUI_Request(App->App,App->window,0,"Dial",
  536.                                                           GetDialString(MSG_BT_ReallyLoad),
  537.                                                           GetDialString(MSG_ReallyLoad))==1)
  538.                                               b=LoadPhoneBook(App);
  539.                                       }
  540.                                       else
  541.                                           b=LoadPhoneBook(App);
  542.  
  543.                                       if (!b)
  544.                                           MUI_Request(App->App,App->window,0,"Dial",
  545.                                                           GetDialString(MSG_BT_OK),
  546.                                                           GetDialString(MSG_NOTLOADED));
  547.                                   }
  548.                                   break;
  549.                         case MEN_SAVE : case ID_SavePB :
  550.                                   {
  551.                                       if (!SavePhoneBook(App))
  552.                                           MUI_Request(App->App,App->window,0,"Dial",
  553.                                                       GetDialString(MSG_BT_OK),
  554.                                                       GetDialString(MSG_NOTSAVED));
  555.                                   }
  556.                                       break;
  557.                         case ID_PBchanged : PBchanged=1; break;
  558.  
  559.                         case MEN_SETDEFAULT :
  560.                                   {
  561.                                       set(App->SL_Length, MUIA_Slider_Level,12);
  562.                                       set(App->SL_Pause, MUIA_Slider_Level,6);
  563.                                   }
  564.                                   break;
  565.  
  566.                         /* Insert your code between the "case" statement and comment "end of case ..." */
  567.                         /* End computing of IDCMP */
  568.  
  569.                         default:
  570.                                 break;
  571.                 }
  572.                 if (running && signal) Wait(signal);
  573.         }
  574.         DisposeApp(App);
  575.         end(0);
  576. }
  577. /* /// */
  578.  
  579.